package com.redhat.auth.exception;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

/**
 *<b>展现层错误处理公共类 for：struts2</b>
 * <br>
 * @author vakin jiang <br />
       mailto:<a href="mailto:chiang.www@gmail.com">chiang.www@gmail.com</a>
 * @version 1.0
            at 2010-9-8
 */
public class WebErrUtil {
    /*默认错误页面路径*/
	private static String DEFAULT_ERRORPAGE_DIR = "/errorpage/";
    /*默认中文错误信息*/
    private static String DEFAULT_ERRORPMSG_CN = "系统维护中,请稍后再试...";
    /*默认英文错误信息*/
    private static String DEFAULT_ERRORPMSG_EN = "System is too busy,Please Wait...";
    
    public static String ALERT = "warn";
	public static String PAGE_404 = "404";
	public static String PAGE_COMMON = "error";
	public static String PAGE_500 = "500";

	
	/**
	 * 输出异常提示信息到页面前端
	 * @param action
	 *              当前action
	 * @param errorType
	 *              错误类型（如果自定义错误类型需要新建如：/errorpage/错误类型.ftl的模版文件）
	 * @param ex
	 *              当前异常
	 * @return
	 */
	public static String buildErrToPage(ActionSupport action,String errorType, Exception e){
		String message = null;
        if (e instanceof BusinessException
                || "RuntimeException".equals(e.getClass().getSimpleName())
                || "RuntimeException".equals(e.getClass().getSuperclass().getSimpleName()))
            message = e.getMessage();
        return buildErrToPage(action, errorType, message);
	}
	
	/**
     * 输出异常提示信息到页面前端
     * @author vakin jiang <br />
           mailto:<a href="mailto:chiang.www@gmail.com">chiang.www@gmail.com</a>
       @date 2010-9-8
     * @param action
     *              当前action
     * @param errorType
     *              错误类型（如果自定义错误类型需要新建如：/errorpage/错误类型.ftl的模版文件）
	 * @param message
	 *              错误信息
	 * @return
	 */
   public static String buildErrToPage(ActionSupport action,String errorType, String message){
       HttpServletRequest request = ServletActionContext.getRequest();
        HttpServletResponse response = ServletActionContext.getResponse();
        message = ErrorResourceService.getMessage(message, request);
        if (message == null || "".equals(message)) {
            message = Locale.CHINA.equals(action.getLocale()) ? DEFAULT_ERRORPMSG_CN
                    : DEFAULT_ERRORPMSG_EN;
        }
        
        if(ALERT.equals(errorType)){
            String json = "{\"actionError\":\"{message}\"}";
            AjaxWebUtil.writeJSON(response, json.replace("{message}", message));
            return null;
        }else{
            action.addActionError(message);
            return getFullPath(DEFAULT_ERRORPAGE_DIR+errorType);
        }
    }

	/**
	 * 输出弹出警告信息到页面
	 * @param action
	 * @param ex
	 * @return
	 */
	public static String buildAlertErrToPage(ActionSupport action, Exception e){
	    return buildErrToPage(action, ALERT, e);
	}
	
	/**
	 * 输出错误信息到公共错误页面(errorpage/error.ftl)
	 * @param action
	 * @param ex
	 * @return
	 */
	public static String buildCommonErrToPage(ActionSupport action, Exception e){
        return buildErrToPage(action, PAGE_COMMON, e);
    }
	
	/**
     * 输出弹出警告信息到页面
     * @param action
     * @param ex
     * @return
     */
    public static String buildAlertErrToPage(ActionSupport action, String message){
        return buildErrToPage(action, ALERT, message);
    }
    
    /**
     * 输出错误信息到公共错误页面(errorpage/error.ftl)
     * @param action
     * @param ex
     * @return
     */
    public static String buildCommonErrToPage(ActionSupport action, String message){
        return buildErrToPage(action, PAGE_COMMON, message);
    }
    
    /**
     * 获取包含ContextPath完整路径
     * @param relativePath
     * @return
     */
    public static String getFullPath(String relativePath) {
        String contextPath = ServletActionContext.getRequest().getContextPath();
        return contextPath
                + (relativePath.startsWith("/") ? relativePath : "/" + relativePath);
    }
}
